package org.djw.user.controller;

import io.swagger.annotations.Api;
import org.djw.basic.exception.BusinessException;
import org.djw.basic.util.AjaxResult;
import org.djw.user.dto.LoginDto;
import org.djw.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 登录接口
 */
@RestController
@RequestMapping("/login")
@Api(tags = "登录接口",description = "登录接口详细描述")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @RequestMapping("/account")
    public AjaxResult login(@RequestBody LoginDto loginDto){

        try {
            Map<String,Object> map = loginService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }

    /**
     * 微信扫码登录接口
     * @param params
     * @return
     */
    @RequestMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> params){

        try {
            return loginService.wechat(params.get("code"));
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }
    /**
     * 微信绑定接口
     * @param params
     * @return
     */
    @RequestMapping("/binder/wechat")
    public AjaxResult binderWechat(@RequestBody Map<String,String> params){

        try {
            Map<String,Object> map = loginService.binderWechat(params);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }
}
